﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Pages
{
    public class PartnerCats:BasePages
    {
         #region "Constructors"
        public PartnerCats(int partnerCatID, DateTime addedDate, string addedBy, string title, int importance, string description, string imageUrl)
		{
            this.ID = partnerCatID;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Importance = importance;
            this.Description = description;
            this.ImageUrl = imageUrl;
        }
		#endregion
		
		#region "Public Properties"
        public string Title { get; set; }

        public int Importance { get; set; }

        public string Description { get; set; }

        public string ImageUrl { get; set; }
		#endregion

        #region Get From PartnerCatsDetail
        private static PartnerCats GetPartnerCatsFromPartnerCatDetails(PartnerCatDetails record)
        {
            if (record == null)
                return null;
            else
            {
                return new PartnerCats(record.ID, record.AddedDate, record.AddedBy,
                   record.Title, record.Importance, record.Description, record.ImageUrl);
            }
        }

        private static List<PartnerCats> GetPartnerCatsListFromPartnerCatDetailsList(List<PartnerCatDetails> recordset)
        {
            List<PartnerCats> categories = new List<PartnerCats>();
            foreach (PartnerCatDetails record in recordset)
                categories.Add(GetPartnerCatsFromPartnerCatDetails(record));
            return categories;
        }

        #endregion

        #region Static Method
        public static List<PartnerCats> ListPartnerCat()
        {
            List<PartnerCats> partnerCats = null;
            string key = "PartnerCats";

            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                partnerCats = (List<PartnerCats>)BizObject.Cache[key];
            }
            else
            {
                List<PartnerCatDetails> recordset = SiteProvider.Pages.ListPartnerCat();
                partnerCats = GetPartnerCatsListFromPartnerCatDetailsList(recordset);
                BasePages.CacheData(key, partnerCats);
            }
            return partnerCats;
        }

        public static PartnerCats GetPartnerCatByID(int PartnerCatsID)
        {
            PartnerCats PartnerCats = null;
            string key = "PartnerCat_" + PartnerCatsID.ToString();

            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                PartnerCats = (PartnerCats)BizObject.Cache[key];
            }
            else
            {
                PartnerCats = GetPartnerCatsFromPartnerCatDetails(SiteProvider.Pages.GetPartnerCat(PartnerCatsID));
                BasePages.CacheData(key, PartnerCats);
            }
            return PartnerCats;
        }

        public static bool UpdatePartnerCat(int id, string title, int importance, string description, string imageUrl)
        {
            PartnerCatDetails record = new PartnerCatDetails(id, DateTime.Now, "", title, importance, description, imageUrl);
            bool ret = SiteProvider.Pages.UpdatePartnerCat(record);
            BizObject.PurgeCacheItems("PartnerCat");
            return ret;
        }

        public static bool DeletePartnerCat(int id)
        {
            bool ret = SiteProvider.Pages.DeletePartnerCat(id);
            //new RecordDeletedEvent("PartnerCats", id, null).Raise();
            BizObject.PurgeCacheItems("PartnerCat");
            return ret;
        }

        public static int InsertPartnerCat(string title, int importance, string description, string imageUrl)
        {
            PartnerCatDetails record = new PartnerCatDetails(0, DateTime.Now,
               BizObject.CurrentUserName, title, importance, description, imageUrl);
            int ret = SiteProvider.Pages.InsertPartnerCat(record);
            BizObject.PurgeCacheItems("PartnerCat");
            return ret;
        }
        #endregion

        #region Method
        public bool Delete()
        {
            bool success = PartnerCats.DeletePartnerCat(this.ID);
            if (success)
                this.ID = 0;
            return success;
        }

        public bool Update()
        {
            return PartnerCats.UpdatePartnerCat(this.ID, this.Title, this.Importance, this.Description, this.ImageUrl);
        }

        #endregion
    }
}
